package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import zhihuilan.dto.MyCircleDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyFactoryFav;
import zhihuilan.entity.MyShop;
import zhihuilan.entity.MyShopHonorCate;
import zhihuilan.mapper.MyShopHonorCateMapper;
import zhihuilan.params.query.MyShopHonorCateQueryParam;
import zhihuilan.service.MyShopHonorCateService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyShopHonorCateServiceImpl extends ServiceImpl<MyShopHonorCateMapper, MyShopHonorCate> implements MyShopHonorCateService {


    @Override
    public WxMiniPageDto<MyShopHonorCate> selectPageList(MyShopHonorCateQueryParam queryParam){
        QueryWrapper<MyShopHonorCate> queryWrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            queryWrapper.eq("shopid", queryParam.getShopid());
        }
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if(pageNum==1){
            pageNum=0;
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyShopHonorCate> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        WxMiniPageDto<MyShopHonorCate> result = new WxMiniPageDto(pageNum, pageSize);
        List<MyShopHonorCate> convertList = page.getRecords().stream().map(po -> {
            MyShopHonorCate dto = new MyShopHonorCate();
            BeanUtils.copyProperties(po,dto);
            return po;
        }).collect(Collectors.toList());
        result.setData(convertList);
        return result;
    }

    @Override
    public List<MyShopHonorCate> selectList(MyShopHonorCate queryParam) {
        QueryWrapper<MyShopHonorCate> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isEmpty(queryParam)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(queryParam.getShopid())) {
            wrapper.eq("shopid", queryParam.getShopid());
        }
        wrapper.orderByAsc("sort");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public MyShopHonorCate selectById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return null;
        }
        return this.baseMapper.selectById(id);
    }

    @Transactional
    @Override
    public MyShopHonorCate addEntity(MyShopHonorCate entity) throws RuntimeException {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (!ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        entity.setSort(this.getMaxSort(entity.getShopid()));
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public MyShopHonorCate updateEntity(MyShopHonorCate entity) {
        if (ObjectUtils.isEmpty(entity)) {
            return null;
        }
        if (ObjectUtils.isEmpty(entity.getId())) {
            return null;
        }
        this.baseMapper.updateById(entity);
        return entity;
    }

    /**
     * 根据主键删除购物车物品
     */
    @Override
    public Boolean deletetById(Integer id) {
        if (ObjectUtils.isEmpty(id)) {
            return false;
        }
        int result = this.baseMapper.deleteById(id);
        return result > 0;
    }

    /**
     * 获取厂家荣誉资质分类的最大排序号；
     */
    private int getMaxSort(Integer shopId) {
        if (ObjectUtils.isEmpty(shopId)) {
            return 0;
        }
        QueryWrapper<MyShopHonorCate> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("max(sort)");
        queryWrapper.eq("shopid", shopId);
        MyShopHonorCate result = this.baseMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(result)) {
            return 0;
        } else {
            return result.getSort() + 1;
        }
    }
}
